﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.iSystem;
using Janus.Windows.GridEX;
using System.Data;

namespace HiStaff.iSecure
{
    public partial class Function : FormBase
    {
        DataTable dtDel;
        public Function()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateCommandBar(CommandBarManager.ActionCommand.Edit,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Save,
                CommandBarManager.ActionCommand.Cancel,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Delete,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);
            var lstMdl = Service.SystemClient.GetModule(new SY_MODULE());
            lstMdl.Insert(0, new SY_MODULE());
            var lstFuncType = Service.SystemClient.GetFunctionType();

            grdData.RootTable.Columns["MODULEID"].HasValueList = true;
            grdData.RootTable.Columns["MODULEID"].ValueList.PopulateValueList(lstMdl, "ID", "NAME");

            grdData.RootTable.Columns["TYPE"].HasValueList = true;
            grdData.RootTable.Columns["TYPE"].ValueList.PopulateValueList(lstFuncType, "ID", "NAME");

            BindData();
            SetToolStripButtonBar(CommandBarManager.ActionCommand.Cancel);
            return base.FormLoad();
        }
        private void BindData()
        {
            DataTable dt = Service.ExcuteStoreToDataTable("APRS_SY_FUNCTION");
            dtDel = dt.Clone();

            grdData.DataSource = dt;
            grdData.Refresh();
        }
        protected override void SetToolStripButtonBar(CommandBarManager.ActionCommand cmd)
        {
            switch (cmd)
            {
                case CommandBarManager.ActionCommand.Edit:
                    SetEnabledCommandSave = true;
                    SetEnabledCommandDelete = true;
                    SetEnabledCommandCancel = true;
                    SetEnabledCommandEdit = false;
                    break;
                case CommandBarManager.ActionCommand.Cancel:
                case CommandBarManager.ActionCommand.Save:
                    SetEnabledCommandSave = false;
                    SetEnabledCommandDelete = false;
                    SetEnabledCommandCancel = false;
                    SetEnabledCommandEdit = true;
                    break;
            }
        }
        protected override bool EditButtonClick()
        {
            grdData.AllowAddNew = InheritableBoolean.True;
            grdData.AllowEdit = InheritableBoolean.True;
            return true;
        }
        protected override bool CancelButtonClick()
        {
            grdData.AllowAddNew = InheritableBoolean.False;
            grdData.AllowEdit = InheritableBoolean.False;
            BindData();
            return true;
        }
        protected override bool SaveButtonClick()
        {
            ExcuteAsynchronous();
            grdData.AllowAddNew = InheritableBoolean.False;
            grdData.AllowEdit = InheritableBoolean.False;
            return true;
        }
        protected override bool DeleteButtonClick()
        {
            if (grdData.SelectedItems.Count == 0) return false;
            GridEXSelectedItem item = grdData.SelectedItems[0];
            if (item.RowType == RowType.Record)
            {
                DataRow dr = ((DataRowView)item.GetRow().DataRow).Row;
                if (dr[HiStaff.iSystem.Data.COL_ID] != DBNull.Value)
                    dtDel.ImportRow(dr);
                ((DataTable)grdData.DataSource).Rows.Remove(dr);
                grdData.Refresh();
            }
            return true;
        }
        protected override bool AsynchronousFunctionWithBGW(System.ComponentModel.BackgroundWorker bgWorker)
        {
            DataTable dt = (DataTable)grdData.DataSource;
            int i = 3;
            int j = 0;
            int percent;
            j++;
            Service.ExcuteStoreNonQuery("APRD_SY_FUNCTION", dtDel);
            percent = (j * 100 / i);
            bgWorker.ReportProgress(percent, "Đang thực hiện");
            j++;
            Service.ExcuteStoreNonQuery("APRU_SY_FUNCTION", dt);
            percent = (j * 100 / i);
            bgWorker.ReportProgress(100, "Hoàn tất");
            return true;
        }
    }
}
